/**
 * 
 */
package net.jadoth.sqlengine.internal.tables;



/**
 * @author Thomas Muenz
 *
 */
public class SqlQualifiedIdentity
{
	private final Sql sql;
	public final Util util = new Util();
	
	

	/**
	 * @param sql
	 */
	public SqlQualifiedIdentity(final String sqlSchema, final String sqlName)
	{
		super();
		this.sql = new Sql(sqlSchema, sqlName);
	}
	public SqlQualifiedIdentity(final Sql sql)
	{
		super();
		this.sql = sql;
	}
	
	
	public Sql sql()
	{
		return this.sql;
	}
	
	/**
	 * @return
	 */
	@Override
	public String toString()
	{
		return this.sql.cachedFullString;
	}
	

	/**
	 * The Class Sql.
	 */
	public static class Sql
	{
		/** The schema. */
		public final String schema;

		/** The name. */
		public final String name;
		
		
		
		
		private transient final String cachedFullString; 

		/**
		 * Instantiates a new sql.
		 *
		 * @param sqlSchema the sql schema
		 * @param sqlName the sql name
		 * @param sqlAlias the sql alias
		 */
		protected Sql(final String sqlSchema, final String sqlName) 
		{
			super();
			this.schema = sqlSchema;
			this.name = sqlName;
			this.cachedFullString = sqlSchema == null || sqlSchema.length() == 0 ?sqlName :sqlSchema+'.'+sqlName;
		}


		/**
		 * @return
		 * @see java.lang.Object#toString()
		 */
		@Override
		public String toString() {
			return this.cachedFullString;
		}
	}
	
	
	public class Util
	{
		/**
		 * Assemble name.
		 *
		 * @param sb the sb
		 * @param withAlias the with alias
		 * @return the string builder
		 */
		public StringBuilder assembleName(final StringBuilder sb)
		{
			return sb.append(SqlQualifiedIdentity.this.sql.toString());
		}
		
	}
	
}
